//
//  MineViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/9.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class MineViewController: UITableViewController {

    @IBOutlet weak var headImageView: UIImageView!
    
    @IBOutlet weak var accountField: UILabel!
    
    @IBAction func logOutAction(_ sender: Any) {
        
        self.alert("确定退出当前账号?", cancelTitle: "取消", cancelBlock: nil, sureTitle: "确定") {
            
            JPUSHService.deleteAlias({ (code, str, c) in
                
            }, seq: 11)
            
            let login = LoginViewController()
            
            let navi = UINavigationController(rootViewController: login)
            
            self.navigationController?.present(navi, animated: true, completion: nil)
        }
    }
    
    @IBAction func messageAction(_ sender: Any) {
        
        alert("暂未开通")
//        let message = VCLoader.vc(with: "Message", storyboardid: "MessageViewController")
//
//        message!.title = "消息"
//
//        self.push(destination: message!)
        
    }
    
    @IBOutlet weak var cacheSizeLbl: UILabel!
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        self.navigationController?.navigationBar.barTintColor = UIColor.hex("2B89DF")
//        self.navigationController?.navigationBar.shadowImage = nil
//        self.navigationController?.navigationBar.isHidden = false
    }
    
    override func viewWillAppear(_ animated: Bool) {
        
        super.viewWillAppear(animated)
        
        self.accountField.text = User.account
        
        self.navigationController?.navigationBar.barTintColor = UIColor.white
//        self.navigationController?.navigationBar.shadowImage = UIImage()
//        self.navigationController?.navigationBar.isHidden = true
        
        CacheManager.getSize { (size) in
            
            let mb = Double(size) / 1024.0 / 1024.0
            
            self.cacheSizeLbl.text = String(format: "%.2f", mb) + "MB"
        }
        
    }
    
    func clearCache() -> Void {
        
        CacheManager.clear()
        
        CacheManager.getSize { (size) in
            
            let mb = Double(size) / 1024.0 / 1024.0
            
            self.cacheSizeLbl.text = String(format: "%.2f", mb) + "MB"
        }
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(image: UIImage(named: "icon_message")!.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(messageAction(_:)))
        self.accountField.text = User.account
        let line = UIView()
        line.backgroundColor = UIColor.white
        self.navigationController?.navigationBar.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.bottom.equalToSuperview().offset(1)
            make.height.equalTo(1)
            make.width.equalToSuperview()
        }
    }
    
    
    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        switch section {
        case 1:
            return 10.0
        default:
            return 0.0
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10.0
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.hex("E1EAF1")
        return view
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        switch indexPath.section {
        case 0:
            if indexPath.row == 0 {
                let info = UserInfoViewController()
                
                info.title = "账户信息"
                
                self.push(destination: info)
            } else if indexPath.row == 1 {
                
                let passwordManager = VCLoader.vc(with: "Main", storyboardid: "PasswordManageViewController") as! PasswordManageViewController
                
                passwordManager.title = "密码管理"
                passwordManager.phone = User.account
                self.push(destination: passwordManager)
            }
            break
        case 1:
            if indexPath.row == 0 {
                clearCache()
            }
            return
        default:
            break
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = UIColor.hex("E1EAF1")
        return view
    }
    
    override var shouldAutorotate: Bool{
        get{
            return false
        }
    }

}
